package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.AssessmentStudyMapper;
import com.zhijian.medical.entity.po.AssessmentStudy;
import com.zhijian.medical.entity.vo.request.AssessmentStudySearchVo;
import com.zhijian.medical.entity.vo.response.AssessmentListVo;
import com.zhijian.medical.entity.vo.response.AssessmentStudyVo;
import com.zhijian.medical.service.AssessmentStudyService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 考核-学习关系表业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2023-02-17 10:54:39
 */
@Slf4j
@Service
public class AssessmentStudyServiceImpl implements AssessmentStudyService {

    @Autowired
    private AssessmentStudyMapper mapper;

    @Override
    public AssessmentStudy selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public AssessmentStudyVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByAssessmentId(Long assessmentId) {
        mapper.deleteByAssessmentId(assessmentId);
    }

    @Override
    public void deleteByStudyId(Long studyId) {
        mapper.deleteByStudyId(studyId);
    }

    @Override
    public Long insert(AssessmentStudy assessmentStudy) {
        Date now = new Date();
        assessmentStudy.setCreateTime(now);
        assessmentStudy.setUpdateTime(now);
        return mapper.insert(assessmentStudy) > 0 ? assessmentStudy.getId() : 0L;
    }

    @Override
    public boolean update(AssessmentStudy assessmentStudy) {
        Date now = new Date();
        assessmentStudy.setUpdateTime(now);
        return mapper.updateByPrimaryKey(assessmentStudy) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(AssessmentStudy record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(AssessmentStudySearchVo assessmentStudySearchVo) {
        return mapper.count(assessmentStudySearchVo);
    }

    @Override
    public List<AssessmentStudyVo> selectList(AssessmentStudySearchVo assessmentStudySearchVo) {
        return mapper.selectList(assessmentStudySearchVo);
    }

    @Override
    public Page<AssessmentStudyVo> selectPage(AssessmentStudySearchVo assessmentStudySearchVo) {
        //先直接查总数
        int count = count(assessmentStudySearchVo);
        //分页
        Page<AssessmentStudyVo> page = new Page<>(true, count, assessmentStudySearchVo.getPageNum(), assessmentStudySearchVo.getPageSize());
        assessmentStudySearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<AssessmentStudyVo> responseVos = this.selectList(assessmentStudySearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<AssessmentStudy> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public List<AssessmentListVo> selectAssessmentListByStudyId(Long studyId) {
        return mapper.selectAssessmentListByStudyId(studyId);
    }

    @Override
    public String selectStudyNamesByAssessmentId(Long assessmentId) {
        return mapper.selectStudyNamesByAssessmentId(assessmentId);
    }

}